package com.fmync.ai.fmync.controller;

import com.fmync.ai.fmync.service.KnowledgeBaseService;
import dev.langchain4j.data.document.Document;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/api/knowledge")
public class KnowledgeBaseController {

    private final KnowledgeBaseService knowledgeBaseService;

    public KnowledgeBaseController(KnowledgeBaseService knowledgeBaseService) {
        this.knowledgeBaseService = knowledgeBaseService;
    }

    @PostMapping("/upload")
    public void uploadDocument(@RequestParam("file") MultipartFile file) throws IOException {
        knowledgeBaseService.addDocument(file);
    }

    @PostMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chat(@RequestParam("question") String question) {
        return knowledgeBaseService.chatStream(question);
    }
} 